<?
class BandMemberDiary extends db {
	
	var $ID, $title, $member_id, $date_time, $body, $is_active;
	
	function getID() {return $this->ID;}
	function getTitle() {return $this->title;}
	function getMemberObject() {
		$mo = BandMember::get($this->member_id);
		return $mo;
	}
	function getMemberID() {
		return $this->member_id;
	}
	function getDateTime($dateMask = null) {
		$dateMask = (!$dateMask) ? DATETIME_FORMAT : $dateMask;
		return date($dateMask, strtotime($this->date_time));
	}
	function getDate() {
		return $this->getDateTime(DATE_FORMAT);
	}
	function getTime() {
		return $this->getDateTime(TIME_FORMAT);
	}	
	function getBody() {return $this->body;}
	function getBodyWordCount() {
		$body = explode(" ", strip_tags($this->body));
		return count($body);
	}
	function isActive() {return $this->is_active;}
	
	function get($diaryID) {
		if (!is_numeric($diaryID) && !$diaryID) {
			return Error::create("Invalid diary ID.");
		}
		
		$q = "select * from Band_Diaries where ID = " . $diaryID;
		$db = new db;
		$r = mysql_query($q);
		
		if (!$r) {
			return Error::MySQL();
		}
		
		$row = mysql_fetch_assoc($r);
		if (!$row['ID']) {
			return Error::create("Invalid diary ID.");
		}
		
		$bd = new BandMemberDiary;
		$bd->ID = $row['ID'];
		$bd->title = $db->sanitize_from_db($row['title']);
		$bd->member_id = $row['member_id'];
		$bd->date_time = $row['date_time'];
		$bd->body = $db->sanitize_from_db($row['body']);
		$bd->is_active = $row['is_active'];
			
		return $bd;
	}
	
	function add($postArray) {

		if (User::isAdmin()) {
			$bm = BandMember::get($postArray['member_id']);
		} else {
			$uo = User::getCurrent();
			$bm = BandMember::getByUserID($uo->getID());
		}
		
		if (db::isError($bm)) {
			return $bm;
		}
				
		$db = new db;
		$member_id = $bm->getID();
		$title = $db->sanitize_to_db($postArray['title']);
		$_dt = strtotime($postArray['date']);
		$dt = date('Y-m-d', $_dt) . ' ' . $postArray['time'];
		$dateTime = date("Y-m-d H:i:s", strtotime($dt));
		$body = $db->sanitize_to_db($postArray['body']);
				
		if (!$title) {
			$title = '(untitled)';
		}
		
		$r = @mysql_query("insert into Band_Diaries (title, member_id, date_time, body, is_active) values ('$title','$member_id','$dateTime','$body'," . DEFAULT_ACTIVE . ")");	
		if (!$r) {
			return Error::MySQL();
		} else {
			$bd = BandMemberDiary::get(mysql_insert_id());
			return $bd;
		}
	}
	
	function update($postArray) {
		if (!$this->canEdit()) {
			return Error::create("You are not allowed to post diary entries to this diary.");
		}
		
		$title = $this->sanitize_to_db($postArray['title']);
		$body = $this->sanitize_to_db($postArray['body']);
		$_dt = strtotime($postArray['date']);
		$dt = date('Y-m-d', $_dt) . ' ' . $postArray['time'];
		$dateTime = date("Y-m-d H:i:s", strtotime($dt));
		if (!$title) {
			$title = '(untitled)';
		}
			
		$r = @mysql_query("update Band_Diaries set title='$title', date_time='$dateTime', body='$body' where ID = " . $this->ID);
		if (!$r) {
			return Error::MySQL();
		}
	}
	
	function canEdit() {
		$uo = User::getCurrent();
		if (is_object($uo)) {
			$bm = $this->getMemberObject();
			return ($uo->getID() == $bm->getUserID() || $uo->isAdmin());
		}
	}
	
	function activate() {
		if ($this->canEdit()) {
			$r = @mysql_query("update Band_Diaries set is_active = 1 where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('You may not activate a post that is not yours.');
		}
	}
	
	function deactivate() {
		if ($this->canEdit()) {
			$r = @mysql_query("update Band_Diaries set is_active = 0 where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('You may not deactivate a post that is not yours.');
		}
	}
	
	function remove() {
		if ($this->canEdit()) {
			$r = @mysql_query("delete from Band_Diaries where ID = " . $this->ID);
			if (!$r) {
				return Error::MySQL();
			}
		} else {
			return Error::create('You may not remove a post that is not yours.');
		}
	}	

}
?>
